//
//  main.m
//  05 - 作业
//
//  Created by 韩旭 on 16/7/8.
//  Copyright © 2016年 基础. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "City.h"
#import "Areas.h"
#import "province.h"
#import "Person.h"
#import "CityDic.h"
#import "PersonModel.h"
#import "CityModel.h"
#import "TempCity.h"
int main(int argc, const char * argv[]) {
    
#pragma mark -
#if 0
    {
        //        一、
        //
        //    1.创建1个包含5个字符串元素的数组；
        //    2.用for循环倒序输出所有元素；
        //    3.用for...in完成所有字符串元素的拼接(生成一个
        //                新的字符串)，并输出拼接好的字符串。
        //      二、
        //
        //    1.创建1个包含5个person对象的数组(姓名用英文)；
        //    2.用for循环找到年龄最大的Person对象
        //    3.用for...in遍历数组，把大于某个年龄的Person对象的姓名后面拼接上字符串“-brother”。
        //      三、
        
        //    1.使用第二题目中生成的数组；
        //    2.使用sortUsingSelector: 分别按年龄，姓名，性别进行排序，输出排序结果；
        //    3.使用NSSortDescriptor，按姓名进行排序。
        //      四、
        //
        //    处理省市区文件中的area.txt
        //    将省市区进行分割处理, 省里包含市, 市里包含区
        //     1. 先用数组字典表示
        //     2. 将数组字典的处理方式转换成数组模型(Class)
        
        //     五、拓展
        //     上述作业做完, 处理Location1.txt
        //        要求同第四题。自己寻找规律
        //     六、如果都做完额外找我要作业, 练习有的是, 就怕你不练
        
    }
#endif
    
#pragma mark - 作业1
#if 0
    {
        NSArray *arrName = @[@"啊蔚", @"小明", @"小龙", @"小红", @"臭狗"];
        for (NSInteger i = arrName.count - 1; i >= 0; i--) {
            NSLog(@"name = %@", arrName[i]);
        }
        NSString *stringApp = [NSMutableString string];
        for (NSString *string in arrName) {
            stringApp = [stringApp stringByAppendingString:string];
        }
        NSLog(@"%@", stringApp);
    }
#endif
#pragma mark - 作业2
#if 0
    {
        //        1
        Person *per = [[Person alloc] initWithName:@"xiaolong" Age:23 Sex:@"女"];
        Person *per1 = [[Person alloc] initWithName:@"awei" Age:12 Sex:@"男"];
        Person *per2 = [[Person alloc] initWithName:@"xiaoming" Age:15 Sex:@"男"];
        Person *per3 = [[Person alloc] initWithName:@"ali" Age:4 Sex:@"变态"];
        Person *per4 = [[Person alloc] initWithName:@"BadDog" Age:44 Sex:@"变态"];
        NSArray *arrPer = [NSArray arrayWithObjects:per, per1, per2, per3, per4, nil];
        NSInteger max = 0;
        NSInteger index = 0;
        for (int i = 0; i < arrPer.count; i++) {
            Person *person = arrPer[i];
            if (max < person.age) {
                max = person.age;
                index = i;
            }
        }
        NSLog(@"年龄最大的对象%@", arrPer[index]);
        
        //        2
        NSString *strName = [NSString string];
        for (Person *perAge in arrPer) {
            if (perAge.age > 20) {
                strName = [strName stringByAppendingString:[NSString stringWithFormat:@"\n%@ -brother\n",perAge.name]];
            }
        }
        NSLog(@"%@", strName);
        
        //        第三题
        // （1）
        NSArray *arrAge = [arrPer sortedArrayUsingSelector:@selector(compareAge:)];
        for (Person *perAge in arrAge) {
            NSLog(@" age = %ld , name = %@ , sex = %@",perAge.age, perAge.name, perAge.sex);
        }
        NSArray *arrName = [arrPer sortedArrayUsingSelector:@selector(compareName:)];
        for (Person *perName in arrName) {
            NSLog(@"age = %ld, name = %@, sex = %@", perName.age, perName.name, perName.sex);
        }
        // （2）
        NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
        arrPer = [arrPer sortedArrayUsingDescriptors:@[sort]];
        NSLog(@"%@", arrPer);
    }
#endif
    
#pragma mark - 第四题
#if 0
    {
        NSString *path = @"/Users/hanxu/DLS160710/Objective-C/HomeWork/05 - 作业/05 - 作业/Homework-05/area.txt";
        NSString *stringPath = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
        //        根据回车切割字符串
        NSArray *arr = [stringPath componentsSeparatedByString:@"\n"];
        //        存放所有的大字典
        //        1.省数组- 省字典- 市数组- 市字典- 区数组- 区字典
        NSMutableArray *arrAll = [NSMutableArray array];
        for (NSString *namePro in arr) {
            //            取到所有的省（取到所有的省）
            if (![namePro hasPrefix:@" "]) {
                //                创建存放所有省的字典（字典中包含两个字段 name array）
                NSMutableDictionary *MutableDic = [NSMutableDictionary dictionary];
                //                添加ProName字段
                [MutableDic setObject:namePro forKey:@"AProvince"];
                //                创建了省中所有市的数组
                NSMutableArray *arrCity = [NSMutableArray array];
                //                添加数组字段
                [MutableDic setObject:arrCity forKey:@"Citys"];
                //                将创建好的省字典, 放到省数组中
                [arrAll addObject:MutableDic];
                //                取到所有的市（两个空格开头, 但不是四个空格开头）
            } else if ([namePro hasPrefix:@"  "] && ![namePro hasPrefix:@"    "]) {
                //                创建所有市的字典
                NSMutableDictionary *dicCitys = [NSMutableDictionary dictionary];
                NSMutableArray *arrArea = [NSMutableArray array];
                [dicCitys setObject:namePro forKey:@" ACityNames"];
                [dicCitys setObject:arrArea forKey:@"Ares"];
                //           难点：  通过lastObject获取到对应的省的字典
                NSDictionary *dicProvince = [arrAll lastObject];
                //                拿到省字典中对应的市数组
                [[dicProvince objectForKey:@"Citys"] addObject:dicCitys];
            } else {
                //                创建区的字典
                NSMutableDictionary *dicArea = [NSMutableDictionary dictionary];
                [dicArea setObject:namePro forKey:@"AreaNames"];
                //                创建 市字典（name 和 区 array）
                //                通过省字典获取到市数组
                NSMutableDictionary *dicPro = [[[arrAll lastObject] objectForKey:@"Citys"] lastObject];
                //                向市数组添加市字典
                //                最终添加到数组中
                [[dicPro objectForKey:@"Ares"] addObject:dicArea];
            }
        }
        //        NSLog(@"%@", arrAll);
        NSMutableArray *arrModel = [NSMutableArray array];
        for (NSDictionary *dicModel in arrAll) {
            PersonModel *model = [[PersonModel alloc] initWithDic:dicModel];
            NSLog(@"%@", model.AProvince);
            [arrModel addObject:model];
            NSArray *arrCity = dicModel[@"Citys"];
            for (NSDictionary *dicCity in arrCity) {
                PersonModel *model = [[PersonModel alloc] initWithDic:dicCity];
                NSLog(@"%@", model.ACityNames);
                [arrModel addObject:model];
                NSArray *arrArea = dicCity[@"Ares"];
                for (NSDictionary *dicArea in arrArea) {
                    PersonModel *model = [[PersonModel alloc] initWithDic:dicArea];
                    NSLog(@"%@", model.AreaNames);
                    [arrModel addObject:model];
                }
            }
        }
    }
#endif
    
    
#pragma mark - 第四题改
#if 1
    {
        NSString *path = @"/Users/hanxu/DLS160710/Objective-C/HomeWork/05 - 作业/05 - 作业/Homework-05/area.txt";
        NSString *stringPath = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
        //        根据回车切割字符串
        NSArray *arr = [stringPath componentsSeparatedByString:@"\n"];
        
        //     2. 省数组 - Province类
        NSMutableArray *allProArray = [NSMutableArray array];
        for (NSString *area in arr) {
            if (![area hasPrefix:@" "]) {
                province *provice = [province areasWithAreasName:area];
                [allProArray addObject:provice];
                NSLog(@"%@", provice.name);
            } else if ([area hasPrefix:@"  "] && ![area hasPrefix:@"    "]) {
                province *pro = [allProArray lastObject];
                City *city = [City areasWithAreasName:area];
                [pro.cityArray addObject:city];
            } else {
                City *city = [[[allProArray lastObject] cityArray] lastObject];
                Areas *areas = [Areas areasWithAreasName:area];
                [city.cityArray addObject:areas];
            }
        }
//        NSLog(@"%@", allProArray);
    }
#endif
    
    
#pragma mark - 第五题
#if 0
    {
        NSString *path = @"/Users/hanxu/DLS160710/Objective-C/HomeWork/05 - 作业/05 - 作业/Homework-05/Location1.txt";
        NSString *stringPath = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
        NSArray *arrString = [stringPath componentsSeparatedByString:@"\n"];
        NSMutableArray *arr1 = [NSMutableArray array];
        NSMutableArray *arrAl = [NSMutableArray array];
        for (NSString *string in arrString) {
            NSArray *arr2 = [string componentsSeparatedByString:@","];
            [arr1 addObject:arr2];
            for (NSString *string2 in arr2) {
                NSArray *arr = [string2 componentsSeparatedByString:@"|"];
                [arrAl addObject:arr];
            }
        }
        NSMutableArray *arrAll = [NSMutableArray array];
        for (NSArray *ar in arrAl) {
            for (NSString *as in ar) {
                [arrAll addObject:as];
            }
        }
        NSMutableArray *arrEncoding = [NSMutableArray array];
        NSMutableArray *arrCity = [NSMutableArray array];
        for (int i = 0; i < arrAll.count ; i++) {
            if ((i + 2) % 2 == 0) {
                [arrCity addObject:arrAll[i]];
            } else {
                [arrEncoding addObject:arrAll[i]];
            }
        }
        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObjects:arrCity forKeys:arrEncoding];
        CityDic *cityDic = [[CityDic alloc] init];
        NSMutableArray *arrALL = [cityDic gruop:dic];
        NSLog(@"%@", arrALL);
        //        2
        NSMutableArray *arrCityModel = [NSMutableArray array];
        for (NSDictionary *dic in arrALL) {
            CityModel *cityModel = [[CityModel alloc] initWithDic:dic];
            NSLog(@"%@", cityModel.AProvince);
            [arrCityModel addObject:cityModel];
            NSMutableArray *arrCitys = dic[@"ArrCitys"];
            for (NSDictionary *dicCitys in arrCitys) {
                CityModel *cityModel = [[CityModel alloc] initWithDic:dicCitys];
                [arrCityModel addObject:cityModel];
                NSLog(@"%@", cityModel.ACitys);
                NSMutableArray *arrArea = dicCitys[@"ArrAreas"];
                for (NSDictionary *dicArea in arrArea) {
                    CityModel *cityModel = [CityModel cityModelDic:dicArea];
                    NSLog(@"%@", cityModel.Areas);
                }
            }
        }
    }
#endif
#pragma mark - 第五题改
#if 0
    {
        NSString *locationPath = @"/Users/hanxu/DLS160710/Objective-C/HomeWork/05 - 作业/05 - 作业/Homework-05/Location1.txt";
        NSString *soure = [NSString stringWithContentsOfFile:locationPath encoding:NSUTF8StringEncoding error:nil];
        //            根据, 分割字符串, 得到所有城市的数组
        NSArray *arrSoure = [soure componentsSeparatedByString:@","];
        NSMutableArray *arrAll = [NSMutableArray array];
        //        根据每一个城市的|处理字符串, 重新生成字典的数组
        for (NSString *city in arrSoure) {
            //            根据|切割字符串
            NSArray *array = [city componentsSeparatedByString:@"|"];
            //            根据切割生成的数组, 生成新的城市字典
            NSMutableDictionary *cityDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:[array firstObject], @"cityName",[array lastObject], @"cityNumber", nil];
            //            添加到行新的数组中
            [arrAll addObject:cityDic];
        }
        //        排序（重要）
        NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"cityNumber" ascending:YES];
        [arrAll sortUsingDescriptors:@[sort]];
        //                NSLog(@"%@", arrAll);
        NSMutableArray *finalArray = [NSMutableArray array];
        for (NSMutableDictionary *cityDic in arrAll) {
            //            NSString *cityName = [cityDic objectForKey:@"cityName"];
            NSString *cityNumber = cityDic[@"cityNumber"];
            if ([cityNumber hasSuffix:@"0000"]) {
                NSMutableDictionary *proDic = [NSMutableDictionary dictionaryWithDictionary:cityDic];
                [proDic setObject:[NSMutableArray array] forKey:@"cityArray"];
                [finalArray addObject:proDic];
                
            } else if ([cityNumber hasSuffix:@"00"] && ![cityNumber hasSuffix:@"0000"]) {
                NSMutableDictionary *proDic = [finalArray lastObject];
                NSMutableArray *cityArray = [proDic objectForKey:@"cityArray"];
                NSMutableDictionary *citydic = [NSMutableDictionary dictionaryWithDictionary:cityDic];
                [citydic setObject:[NSMutableArray array] forKey:@"AreaArray"];
                [cityArray addObject:citydic];
            } else {
                //                NSMutableArray *zoneArr = [[[[finalArray lastObject] objectForKey:@"cityArray"] lastObject] objectForKey:@"AreaArray"];
                //              注意：写代码尽量分开写
                NSMutableDictionary *arrCity = [finalArray lastObject];
                NSMutableArray *arrAreas = arrCity[@"cityArray"];
                NSMutableDictionary *dicCity = [arrAreas lastObject];
                NSMutableArray *zoneArray = [dicCity objectForKey:@"AreaArray"];
                NSMutableDictionary *zoneDic = [NSMutableDictionary dictionaryWithDictionary:cityDic];
                [zoneArray addObject:zoneDic];
            }
        }
        NSLog(@"%@", finalArray);
    }
    
#endif
    
#pragma mark - 第五题（改）2
#if 1
    {
        NSString *locationPath = @"/Users/hanxu/DLS160710/Objective-C/HomeWork/05 - 作业/05 - 作业/Homework-05/Location1.txt";
        NSString *soure = [NSString stringWithContentsOfFile:locationPath encoding:NSUTF8StringEncoding error:nil];
        //            根据, 分割字符串, 得到所有城市的数组
        NSArray *arrSoure = [soure componentsSeparatedByString:@","];
        NSMutableArray *arrAll = [NSMutableArray array];
        //        根据每一个城市的|处理字符串, 重新生成字典的数组
        for (NSString *city in arrSoure) {
            //            根据|切割字符串
            NSArray *array = [city componentsSeparatedByString:@"|"];
            //            根据切割生成的数组, 生成新的城市字典
            TempCity *tempCity = [[TempCity alloc] initWithName:[array firstObject] number:[array lastObject]];
            //            添加到行新的数组中
            [arrAll addObject:tempCity];
        }
        //        对 类数组 排序
        NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"cityNumber" ascending:YES];
        [arrAll sortUsingDescriptors:@[sort]];
        //        NSLog(@"%@", arrAll);
        NSMutableArray *finalClassArray = [NSMutableArray array];
        for (TempCity *tempCity in arrAll) {
            if ([tempCity.cityNumber hasSuffix:@"0000"]) {
                province *pro = [province zoneWithTempCity:tempCity];
                [finalClassArray addObject:pro];
                NSLog(@"%@", pro);
            } else if ([tempCity.cityNumber hasSuffix:@"00"] && ![tempCity.cityNumber hasSuffix:@"000"]) {
                province *pro = [finalClassArray lastObject];
                City *city = [City zoneWithTempCity:tempCity];
                [pro.cityArray addObject:city];
            } else {
                province *pro = [finalClassArray lastObject];
                City *city = [pro.cityArray lastObject];
                Areas *areas = [Areas zoneWithTempCity:tempCity];
                [city.cityArray addObject:areas];
            }
        }
//        NSLog(@"%@", finalClassArray);
    }
#endif
    
    
#pragma mark -
#if 0
    {
        
        
        //        时间戳
        -(NSString *)getTiampChangerTimer:(NSString *)str{
            NSDateFormatter *forMatter = [[NSDateFormatter alloc]init];
            [forMatter setDateStyle:NSDateFormatterMediumStyle];
            [forMatter setTimeStyle:NSDateFormatterMediumStyle];
            [forMatter setDateFormat:@"yyyy.MM.dd"];
            NSInteger time = [str intValue];
            NSDate *dataTime = [NSDate dateWithTimeIntervalSince1970:time];
            NSString *string = [forMatter stringFromDate:dataTime];
            return string;
            
            
        }
        
        //        时间转化
        - (NSString *)deltaTime{
            NSDate *timeXT = [NSDate dateWithTimeIntervalSinceNow:0];
            NSDate *publish = [NSDate dateWithTimeIntervalSince1970:[self.model.publish_time intValue]];
            NSTimeInterval nowTime = [timeXT timeIntervalSince1970];
            NSTimeInterval publishTime = [publish timeIntervalSince1970];
            NSString *string = @"";
            NSTimeInterval deltaTime = nowTime - publishTime;
            if (deltaTime / 3600 < 1) {
                string = [NSString stringWithFormat:@"%f", deltaTime / 60];
                string = [string substringToIndex:string.length - 7];
                if ([string isEqualToString:@"0"]) {
                    return string = @"刚刚";
                }
                else{
                    string=[NSString stringWithFormat:@"%@分钟前", string];
                    return string;
                }
            }
            if (deltaTime / 3600 > 1 && deltaTime / 86400 < 1) {
                string = [NSString stringWithFormat:@"%f", deltaTime / 3600];
                string = [string substringToIndex:string.length - 7];
                string=[NSString stringWithFormat:@"%@小时前", string];
                return string;
            }
            if (deltaTime / 86400 > 1) {
                string = [NSString stringWithFormat:@"%f", deltaTime / 86400];
                string = [string substringToIndex:string.length - 7];
                string=[NSString stringWithFormat:@"%@天前", string];
                return string;
            }
            return 0;
        }
    }
#endif
    
    return 0;
}
